<?php

/**
 *TrainerCity 商品管理程序 *
 * $Author: wangleisvn $
 * $Id: goods.php 17114 2010-04-16 07:13:03Z wangleisvn $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
include_once(ROOT_PATH . '/includes/lib_main.php');

$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table('goods'), $db, 'goods_id', 'goods_name');

/*------------------------------------------------------ */
//-- Projects List
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{	
	#add by wanglei
	$sql = 'SELECT a.*,b.budgetType,u.user_name,g.genderType ' .'FROM ' . $GLOBALS['ecs']->table('projects') . ' AS a JOIN '. $GLOBALS['ecs']->table('budget') . ' AS b JOIN ' .$GLOBALS['ecs']->table('users') . ' AS u JOIN ' .$GLOBALS['ecs']->table('gender')." AS g "." WHERE  a.budget=b.num AND a.uid=u.user_id AND u.sex=g.xid ";
    $projects = $GLOBALS['db']->getAll($sql);
	
	$taskcount=count($projects);
		
	$record_count = $taskcount;
	$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
	$pagesize = 10;
	$pager = get_pager('projects.php', array('act' => 'list'), $record_count, $page, $pagesize);
	$fitness_lists = get_msg_list($pagesize, $pager['start']);
	
	
	$smarty->assign('projects_list',   $fitness_lists);
	#end by wanglei
    $smarty->assign('pager', $pager);
    $smarty->assign('full_page',    1);
	$smarty->assign('action_link',      array('text' => 'Add New Project', 'href' => 'projects.php?act=add_project'));
	$smarty->display('projects_list.htm');
}


/*------------------------------------------------------ */
//-- Project Detail
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'project_detail')
{
	/*获取可选择信息*/
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainertype');
	$trainertype=$db->getAll($sql);

	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainingtype');
	$trainingtype=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionstype');
	$sessionstype=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionlength');
	$sessionlength=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('budget');
	$budget=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('quoting');
	$quoting=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('friendsnum');
	$friendsnum=$db->getAll($sql);
	
	/*获取项目id*/
	$getpid  = isset($_REQUEST['pid']) ? trim($_REQUEST['pid']) : 'nothing';
	
	#获取任务详细信息
	$sql = 'SELECT a.*,b.budgetType,u.user_name,g.genderType ' .'FROM ' . $GLOBALS['ecs']->table('projects') . ' AS a JOIN '. $GLOBALS['ecs']->table('budget') . ' AS b JOIN ' .$GLOBALS['ecs']->table('users') . ' AS u JOIN ' .$GLOBALS['ecs']->table('gender')." AS g "." WHERE a.pid='$getpid' AND a.budget=b.num AND a.uid=u.user_id AND u.sex=g.xid ";
	$project=$db->getRow($sql);
		
	$smarty->assign('trainertype', $trainertype);
	$smarty->assign('trainingtype', $trainingtype);
	$smarty->assign('sessionstype', $sessionstype);
	$smarty->assign('sessionlength', $sessionlength);
	$smarty->assign('budget', $budget);
	$smarty->assign('quoting', $quoting);
	$smarty->assign('friendsnum', $friendsnum);
	$smarty->assign('project', $project);
	$smarty->display('project_info.htm');
	   
}

/*------------------------------------------------------ */
//-- Add New Project
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add_project')
{
	/*获取可选择信息*/
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainertype');
	$trainertype=$db->getAll($sql);

	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('trainingtype');
	$trainingtype=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionstype');
	$sessionstype=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('sessionlength');
	$sessionlength=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('budget');
	$budget=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('quoting');
	$quoting=$db->getAll($sql);
	
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('friendsnum');
	$friendsnum=$db->getAll($sql);
	
	$sql = "SELECT user_id,user_name FROM " .$GLOBALS['ecs']->table('users')." WHERE usertype=2 ";
	$users=$db->getAll($sql);

	$smarty->assign('trainertype', $trainertype);
	$smarty->assign('trainingtype', $trainingtype);
	$smarty->assign('sessionstype', $sessionstype);
	$smarty->assign('sessionlength', $sessionlength);
	$smarty->assign('budget', $budget);
	$smarty->assign('quoting', $quoting);
	$smarty->assign('friendsnum', $friendsnum);
	$smarty->assign('users', $users);
	$smarty->display('project_add.htm');
}

/*------------------------------------------------------ */
//-- Add New Project
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'act_add_project')
{
	$project_subject = !empty($_POST['project_subject']) ? trim($_POST['project_subject']) : '';
	$user_id = !empty($_POST['user_id']) ? intval($_POST['user_id']) : '';
	$trainer_type = !empty($_POST['trainer_type']) ? intval($_POST['trainer_type']) : 0;
	$training_type = !empty($_POST['training_type']) ? intval($_POST['training_type']) : 0;
	$session_type = !empty($_POST['session_type']) ? intval($_POST['session_type']) : 0;
	$session_length = !empty($_POST['session_length']) ? intval($_POST['session_length']) : 0;
	$budget_type = !empty($_POST['budget_type']) ? intval($_POST['budget_type']) : 0;
	$quoting_type = !empty($_POST['quoting_type']) ? intval($_POST['quoting_type']) : 0;
	$city_town = !empty($_POST['city_town']) ? trim($_POST['city_town']) : '';
	$friends_num = !empty($_POST['friends_num']) ? intval($_POST['friends_num']) : 0;
	$project_details = !empty($_POST['project_details']) ? trim($_POST['project_details']) : '';
	$feature_flag = !empty($_POST['feature_flag']) ? intval($_POST['feature_flag']) : 0;

	
	#if($project_subject==NULL || $user_id==NULL || $trainer_type==NULL || $training_type==NULL || $session_type==NULL || $session_length==NULL || $budget_type==NULL || $quoting_type==NULL || $city_town==NULL || $friends_num==NULL || $project_details==NULL || $feature_flag==NULL)
	#{
		/*Add Failed*/		
	#	$links = array(array('href' => 'projects.php?act=add_project', 'text' => "Trainer Type List"));
    #    sys_msg("There are something you forget to input, please try again!", 0, $links);
	#}
	#else
	#{
		$fctime= date("Y-m-d H:i:s",time());
		if($quoting_type == 1)
		{
			$endtime= time() + 14*24*60*60;
		}elseif($quoting_type == 2)
		{
			$endtime= time() + 30*24*60*60;
		}
		$endtime= date("Y-m-d H:i:s",$endtime);	

		/*任务信息入库*/	
		$sql = "INSERT INTO " . $ecs->table('projects') . "(uid,subject,trainertype,trainingtype,sessions,length,budget,quoting,city,friends,content,featuredFlag,createdDate,endDate) VALUES ('" . $user_id . "', '" . $project_subject . "', '" . $trainer_type . "', '" . $training_type . "','" . $session_type . "', '" . $session_length . "', '" . $budget_type . "','" . $quoting_type . "', '" . $city_town . "', '" . $friends_num . "', '" . $project_details . "', '" . $feature_flag . "', '" . $fctime . "', '" . $endtime . "')";
   		if($db->query($sql) != false)
		{
			/*Add Successfully*/	
			$links = array(array('href' => 'projects.php?act=add_project', 'text' => "Trainer Type List"));
        	sys_msg("New Type Add Successfully!", 0, $links);
		}
		else
		{
			/*Add Failed*/	
			$links = array(array('href' => 'projects.php?act=add_project', 'text' => "Trainer Type List"));
       		sys_msg("Failed, please try again!", 0, $links);
		}
	#}
}

/*------------------------------------------------------ */
//-- Proposal Delete
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'project_delete')
{
	/*获取方案id*/
	$getpid  = isset($_REQUEST['pid']) ? trim($_REQUEST['pid']) : 'nothing';
	
	#add by wanglei
	$sql = "DELETE FROM " . $GLOBALS['ecs']->table('projects') .
            " WHERE pid='$getpid' ";
	if($db->query($sql) != false)
	{
		/*Delete Successfully*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Delete Project Successfully!", 0, $links);
			
	}
	else
	{
		/*Delete Failed*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Failed, please try again!", 0, $links);
	}	
}


/*------------------------------------------------------ */
//-- Proposal Delete
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'projects_delete')
{
	/*获取方案id*/
	$getpids  = $_REQUEST['checkboxes'];
	
	#add by wanglei
	$pids = '';
	for($i = 0; $i < count($getpids) - 1; $i++)
	{		
		$pids = $pids . $getpids[$i] . ',';
	}
	$pids = $pids . $getpids[count($getpids) - 1];
	
	$sql = "DELETE FROM " . $GLOBALS['ecs']->table('projects') .
            " WHERE pid IN ($pids) ";
	if($db->query($sql) != false)
	{
		/*Delete Successfully*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Delete Project Successfully!", 0, $links);
			
	}
	else
	{
		/*Delete Failed*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Failed, please try again!", 0, $links);
	}	
}

/*------------------------------------------------------ */
//-- Project Modify
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'modify')
{
	/*Get Project ID*/
	$getpid  = isset($_REQUEST['pid']) ? trim($_REQUEST['pid']) : 'nothing';
	
	$project_subject = !empty($_POST['project_subject']) ? trim($_POST['project_subject']) : '';
	$trainer_type = !empty($_POST['trainer_type']) ? intval($_POST['trainer_type']) : 0;
	$training_type = !empty($_POST['training_type']) ? intval($_POST['training_type']) : 0;
	$sessions_type = !empty($_POST['sessions_type']) ? intval($_POST['sessions_type']) : 0;
	$session_length = !empty($_POST['session_length']) ? intval($_POST['session_length']) : 0;
	$budget_type = !empty($_POST['budget']) ? intval($_POST['budget']) : 0;
	$quoting_type = !empty($_POST['quoting']) ? intval($_POST['quoting']) : 0;
	$city_town = !empty($_POST['city_town']) ? trim($_POST['city_town']) : '';
	$friends_num = !empty($_POST['friends_num']) ? intval($_POST['friends_num']) : 0;
	$project_details = !empty($_POST['project_detail']) ? trim($_POST['project_detail']) : '';
	$create_date = !empty($_POST['create_date']) ? trim($_POST['create_date']) : '';
	$special_request = !empty($_POST['special_request']) ? trim($_POST['special_request']) : '';
	$feature_flag = !empty($_POST['feature_flag']) ? intval($_POST['feature_flag']) : 0;
	$feature_date = !empty($_POST['feature_date']) ? trim($_POST['feature_date']) : '';
	$proposals_num = !empty($_POST['proposals_num']) ? trim($_POST['proposals_num']) : '';
	$project_status = !empty($_POST['project_status']) ? trim($_POST['project_status']) : '';	
	
	$fctime= strtotime($create_date);
	if($quoting_type == 1)
	{
		$endtime= $fctime + 14*24*60*60;
	}elseif($quoting_type == 2)
	{
		$endtime= $fctime + 30*24*60*60;
	}
	$endtime= date("Y-m-d H:i:s",$endtime);	
	
	$sql = "UPDATE " . $GLOBALS['ecs']->table('projects') .
            " SET subject='$project_subject', trainerType='$trainer_type',trainingType='$training_type', sessions='$sessions_type', length='$session_length', budget='$budget_type' ,quoting='$quoting_type', city='$city_town', createdDate='$create_date', endDate='$endtime', friends='$friends_num', content='$project_details', specialReq='$special_request', featuredFlag='$feature_flag', proposalsNum='$proposals_num', status='$project_status' WHERE num='$getpid' ";
	if($db->query($sql) != false)
	{
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Modify Project Successfully!", 0, $links);
			
	}
	else
	{
		/*Delete Failed*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Failed, please try again!", 0, $links);
	}	
}

elseif ($_REQUEST['act'] == 'today_feature')
{	
	#add by wanglei
	$fctime= date("Y-m-d",time());
	
	$sql = 'SELECT * ' .'FROM ' . $GLOBALS['ecs']->table('projects') . " WHERE  apply_feature=1 AND createdDate='$fctime' ";
    $projects = $GLOBALS['db']->getAll($sql);
	$smarty->assign('projects_list',   $projects);
	#end by wanglei
    
    $smarty->assign('full_page',    1);
	$smarty->assign('action_link',      array('text' => 'Add New Project', 'href' => 'projects.php?act=add_project'));
	$smarty->display('features_list.htm');
}

elseif ($_REQUEST['act'] == 'act_feature')
{	
	/*Get Project ID*/
	$getfeaturepid  = isset($_POST['feature']) ? intval($_POST['feature']) : 0;
	
	if($getfeaturepid == 0)
	{
		/*Delete Successfully*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Feature Project Failed!", 0, $links);
	}
	
	#add by wanglei
	$fctime= date("Y-m-d",time());
	
	$sql = 'UPDATE ' . $GLOBALS['ecs']->table('projects') . " SET featuredFlag=0 "." WHERE createdDate='$fctime'  ";
	if(!$GLOBALS['db']->query($sql))
	{
		/*Delete Successfully*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Feature Project Failed,please try again!", 0, $links);
	}
		
	$sql = 'UPDATE ' . $GLOBALS['ecs']->table('projects') . " SET featuredFlag=1, featuredDate='$fctime' "." WHERE  pid='$getfeaturepid' ";

    if($GLOBALS['db']->query($sql))
	{
		/*Delete Successfully*/	
		$links = array(array('href' => 'projects.php?act=list', 'text' => "Projects List"));
        sys_msg("Feature Project Successfully!", 0, $links);
	}
}

/**
 * 
 *
 * @param   integer $num
 * @param   integer $start
 *
 * @return  array
 */
function get_msg_list($num, $start, $temp='', $sort='pid')
{
    /*   */	
	$sql = 'SELECT a.*,b.budgetType,u.user_name, u.user_id, u.firstname, u.lastname, g.genderType ' .'FROM ' . $GLOBALS['ecs']->table('projects') . ' AS a JOIN '. $GLOBALS['ecs']->table('budget') . ' AS b JOIN ' .$GLOBALS['ecs']->table('users') . ' AS u JOIN ' .$GLOBALS['ecs']->table('gender')." AS g "." WHERE a.budget=b.num AND a.uid=u.user_id AND u.sex=g.xid AND a.status != 0 ".$temp;
	$sql = $sql." ORDER by ". $sort." DESC LIMIT ".$start.",".$num;
	$res = $GLOBALS['db']->getAll($sql);
	
	/* Caculate The Remaining Time of The Project */
	for($i=0; $i<count($res); $i++)
	{
		$createtime=strtotime($res[$i]['createdDate']);
		
		if($res[$i].quoting == 1)
		{
			$endtime = $createtime + 14 * 86400;
		}
		else
		{
			$endtime = $createtime + 30 * 86400;
		}
	
		$nowtime=time();
    	$days=($endtime - $nowtime)/86400;
    	$new_day=explode('.',$days);
	
    	$hours=floor(($endtime - $nowtime)/3600);

   	 	$hours_day=$hours-($new_day[0]*24);

    	$minutes=floor(($endtime - $nowtime)/60);

    	if($new_day[0]>0){
       		 $temp="$new_day[0] days $hours_day hours";
    	}else if($hours>0){
      		 $temp="$hours hours";
   		}else if($hours_day==0){
      		 $temp="$minutes minutes";
    	}else if($hours_day<0){
     		 $temp="over";
    	}
		$res[$i]['lefttime']=$temp;		
	}
	
    return $res;
}


?>